package com.lancoo.answer.view.result

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.answer.R
import com.lancoo.answer.databinding.EvAdapterAnswersheetChildBinding
import com.lancoo.answer.model.bean.SameQuesDetail
import com.lancoo.answer.view.base.XBaseRecyclerAdapter
import com.lancoo.answer.view.base.XBaseRecyclerHolder

/**
 * auther: 王国龙
 * Date：2020.06.15
 * describe:试卷预览对应的答题卡
 */
class SameAnswerSheetChildAdapter(
    val sameQuesDetail: SameQuesDetail,
    val recommendState: Int
) : XBaseRecyclerAdapter<EvAdapterAnswersheetChildBinding>() {

    override fun onBindingView(viewGroup: ViewGroup?): EvAdapterAnswersheetChildBinding {
        return EvAdapterAnswersheetChildBinding.inflate(
            LayoutInflater.from(
                viewGroup!!.context
            ), viewGroup, false
        )
    }

    override fun onBindingData(
        holder: XBaseRecyclerHolder<EvAdapterAnswersheetChildBinding>,
        position: Int
    ) {
        var index_A = position_return_indexA(sameQuesDetail, position)
        var index_B = position_return_indexB(sameQuesDetail, position)
        var index_C = position_return_indexC(sameQuesDetail, position)
        val isAnswer = sameQuesDetail[index_A].TkModel.ChildList[index_B].ItemList[index_C].IsAnswer
        val state = sameQuesDetail[index_A].TkModel.ChildList[index_B].ItemList[index_C].State
        val scoreRate =
            sameQuesDetail[index_A].TkModel.ChildList[index_B].ItemList[index_C].ScoreRate
        holder.viewBinding.TvSortIndex.text = (position + 1).toString()
        if (state <= 0) {
            if (isAnswer) {
                holder.viewBinding.TvSortIndex.setBackgroundResource(R.mipmap.ev_icon_choice_blue)
                holder.viewBinding.TvSortIndex.setTextColor(Color.WHITE)
            } else {
                holder.viewBinding.TvSortIndex.setBackgroundResource(R.mipmap.ev_icon_choice_white)
                holder.viewBinding.TvSortIndex.setTextColor(Color.parseColor("#333333"))
            }
        } else {
            if (scoreRate >= 0.6) {
                holder.viewBinding.TvSortIndex.setBackgroundResource(R.mipmap.ev_icon_choice_green)
                holder.viewBinding.TvSortIndex.setTextColor(Color.WHITE)
            } else {
                holder.viewBinding.TvSortIndex.setBackgroundResource(R.mipmap.ev_icon_choice_red)
                holder.viewBinding.TvSortIndex.setTextColor(Color.WHITE)
            }
        }
        holder.viewBinding.TvSortIndex.setOnClickListener {
            clickChilidListener!!.onClick((position + 1).toString())
        }
    }

    override fun getItemCount(): Int {
        var itemCount = 0
        var sameQuesDetailSize = sameQuesDetail.size
        for (a in 0 until sameQuesDetailSize) {
            itemCount += sameQuesDetail[a].TkModel.QuesChildNum
        }
        return itemCount
    }

    private fun position_return_indexA(sameQuesDetail: SameQuesDetail, position: Int): Int {
        var postionX = -1
        var index_A = 0
        var sameQuesDetailSize = sameQuesDetail.size
        outLoop@ for (a in 0 until sameQuesDetailSize) {
            var ChildListSize = sameQuesDetail[a].TkModel.ChildList.size
            for (b in 0 until ChildListSize) {
                val ItemListSize = sameQuesDetail[a].TkModel.ChildList[b].ItemList.size
                for (c in 0 until ItemListSize) {
                    postionX += 1
                    if (position == postionX) {
                        index_A = a
                        break@outLoop
                    }
                }
            }
        }
        return index_A
    }

    private fun position_return_indexB(sameQuesDetail: SameQuesDetail, position: Int): Int {
        var postionX = -1
        var index_B = 0
        var sameQuesDetailSize = sameQuesDetail.size
        outLoop@ for (a in 0 until sameQuesDetailSize) {
            var ChildListSize = sameQuesDetail[a].TkModel.ChildList.size
            for (b in 0 until ChildListSize) {
                val ItemListSize = sameQuesDetail[a].TkModel.ChildList[b].ItemList.size
                for (c in 0 until ItemListSize) {
                    postionX += 1
                    if (position == postionX) {
                        index_B = b
                        break@outLoop
                    }
                }
            }
        }
        return index_B
    }

    private fun position_return_indexC(sameQuesDetail: SameQuesDetail, position: Int): Int {
        var postionX = -1
        var index_C = 0
        var sameQuesDetailSize = sameQuesDetail.size
        outLoop@ for (a in 0 until sameQuesDetailSize) {
            var ChildListSize = sameQuesDetail[a].TkModel.ChildList.size
            for (b in 0 until ChildListSize) {
                val ItemListSize = sameQuesDetail[a].TkModel.ChildList[b].ItemList.size
                for (c in 0 until ItemListSize) {
                    postionX += 1
                    if (position == postionX) {
                        index_C = c
                        break@outLoop
                    }
                }
            }
        }
        return index_C
    }

    interface OnClickChildListener {
        fun onClick(sortIndex: String)
    }

    private var clickChilidListener: OnClickChildListener? = null

    fun setClickChildListener(clickChilidListener: OnClickChildListener?) {
        this.clickChilidListener = clickChilidListener
    }
}